﻿Public Class frm_main

    Dim i As Integer

    Dim ValidSerial As Boolean = False

    Private Sub frm_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' load setting
        Call load_Setting()

        ' load jenjang
        With cmb_jenjang.Items
            .Add("SMP")
            .Add("SMA")
            .Add("SMK")
        End With
        cmb_jenjang.SelectedIndex = 0

        pct_invalid.Location = pct_valid.Location

    End Sub

    Private Sub cmb_jenjang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_jenjang.SelectedIndexChanged

        Select Case cmb_jenjang.Text
            Case "SMP"
                ListSekolah = New cls_list
                Call init_List_SMP()
                cmb_sekolah.Items.Clear()

                For Me.i = 0 To ListSekolah.List.Count - 1
                    cmb_sekolah.Items.Add(ListSekolah.List.Item(i).Nama)
                Next

                cmb_sekolah.SelectedIndex = 0
                APP.Info.Jenjang = "SMP"

            Case "SMA"
                ListSekolah = New cls_list
                Call init_List_SMA()
                cmb_sekolah.Items.Clear()

                For Me.i = 0 To ListSekolah.List.Count - 1
                    cmb_sekolah.Items.Add(ListSekolah.List.Item(i).Nama)
                Next

                cmb_sekolah.SelectedIndex = 0
                APP.Info.Jenjang = "SMA"

            Case "SMK"
                ListSekolah = New cls_list
                Call init_List_SMK()
                cmb_sekolah.Items.Clear()

                For Me.i = 0 To ListSekolah.List.Count - 1
                    cmb_sekolah.Items.Add(ListSekolah.List.Item(i).Nama)
                Next

                cmb_sekolah.SelectedIndex = 0
                APP.Info.Jenjang = "SMK"

        End Select


    End Sub

    Private Sub cmb_sekolah_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_sekolah.SelectedIndexChanged
        APP.Info.KD_Sekolah = ListSekolah.getKode(cmb_sekolah.Text)
    End Sub

    Private Sub cmd_simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_simpan.Click

        '# Cek Serial
        If ValidSerial = False Then
            MsgBox("Nomor Serial yang dimasukan tidak benar!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
            Exit Sub
        End If

        '# Cek Server
        If Connection.openConnection = False Then
            MsgBox("Tidak bisa terhubung dengan server", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
            Exit Sub
        End If

        '# buat akun
        If chk_buat_akun.Checked = True Then

            Validation.clearRules()
            Validation.addRules(txt_nama_akun.Text, "Nama Akun", "required|length[3-20]|username")
            Validation.addRules(txt_sandi.Text, "Kata Sandi", "required|length[3-10]|password")
            Validation.addRules(txt_ulang_sandi.Text, "Ulangi Kata Sandi", "matches[Kata Sandi]")

            If Validation.isValid Then
                ' cek akun, bisi aya
                DB.FlushCache()
                DB.Selects("*")
                DB.From("tbl_pengguna")
                DB.Where("id_pengguna", txt_nama_akun.Text)

                Connection.ExecuteToDataReader(DB.GetQueryString)

                If Connection.Read.HasRows Then
                    MsgBox("Nama Akun : " & txt_nama_akun.Text & " sudah terdapat dalam database!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                    Exit Sub
                End If

                DB.FlushCache()
                DB.Insert("tbl_pengguna")
                DB.SetField("id_pengguna", txt_nama_akun.Text)
                DB.SetField("kata_sandi", Encript.GetMD5Hash(txt_sandi.Text))

                Connection.ExecuteNonQuery(DB.GetQueryString)

            Else
                Validation.showMessage()
                Exit Sub
            End If
            
        End If

        '# save config
        Call save_Setting()
        MsgBox("Pengaturan Berhasil", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
        End

    End Sub

    Private Sub msk_serial_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles msk_serial.TextChanged
       
        If msk_serial.MaskCompleted = True Then
            '# cek serial
            Dim serial As String
            serial = GenerateKey(APP.Info.KD_Sekolah)

            If serial = msk_serial.Text Then
                ValidSerial = True
                APP.Info.Serial = serial
                pct_valid.Visible = True
            Else
                ValidSerial = False
                pct_invalid.Visible = True
            End If

            ' disable jenjang dan sekolah
            cmb_jenjang.Enabled = False
            cmb_sekolah.Enabled = False

        Else
            ValidSerial = False
            pct_valid.Visible = False
            pct_invalid.Visible = False

            ' disable jenjang dan sekolah
            cmb_jenjang.Enabled = True
            cmb_sekolah.Enabled = True
        End If
    End Sub

    Private Sub msk_serial_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles msk_serial.MaskInputRejected

    End Sub

    Private Sub cmd_cek_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cek.Click
        If Connection.openConnection = True Then
            MsgBox("Koneksi ke server sukses...", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
        End If
    End Sub

    Private Sub chk_buat_akun_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_buat_akun.CheckedChanged
        txt_nama_akun.Enabled = chk_buat_akun.Checked
        txt_sandi.Enabled = chk_buat_akun.Checked
        txt_ulang_sandi.Enabled = chk_buat_akun.Checked
    End Sub

    Private Sub txt_IP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_IP.TextChanged
        APP.Info.Host = txt_IP.Text
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As String = GenerateKey(APP.Info.KD_Sekolah)
        MsgBox(sr)
        My.Computer.Clipboard.SetText(sr)
    End Sub
End Class